package com.example.administrator.a0023adapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView lv;
    private ArrayAdapter<String> adapter;

    /*
        ArrayAdapter 使用在适配每一项都是字符串的情况

     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化 ListView
        lv = findViewById(R.id.lv);

        //初始化数据源
        List<String> list  = new ArrayList<>();
        for (int i=0;i<50;i++){
            list.add("数据"+i);
        }

        //适配初始化
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);

        lv.setAdapter(adapter);


        //listView item单击事件

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //1.获取listview 里的值
                TextView textView = view.findViewById(android.R.id.text1);
                String _id = textView.getText().toString();
                Toast.makeText(MainActivity.this,"heheh"+_id,Toast.LENGTH_LONG).show();

                //2.获取listview 里的值,数据源用map 的情况, 数据源什么类型就用什么类型
                //Map item = (Map)parent.getItemAtPosition(position);
                //Toast.makeText(MainActivity.this,"heheh"+item.get("_id"),Toast.LENGTH_LONG).show();
                //Toast.makeText(MainActivity.this,"hehhah"+id,Toast.LENGTH_LONG).show();
            }
        });

    }
}
